<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:hf="http://happyfaces.org/happyfaces" xmlns:bind="http://happyfaces.org/bind"
	xmlns:c="http://java.sun.com/jsp/jstl/core">

<ui:composition>

	<c:if test="${empty required}">
		<c:set var="required" value="false" />
	</c:if>

	<!-- Show form field -->
	<c:if test="${empty show}">
		<c:set var="show" value="true" />
	</c:if>
	<c:if test="${!empty objectId and !empty showOnlyOnNew}">
		<c:set var="show" value="false" />
	</c:if>
	<c:if test="${empty objectId and !empty showOnlyOnNew}">
		<c:set var="show" value="true" />
	</c:if>
	<c:if test="${empty objectId and !empty showOnlyOnConsult}">
		<c:set var="show" value="false" />
	</c:if>

	<!-- Default size -->
	<c:if test="${empty size}">
		<c:set var="size" value="20" />
	</c:if>
	<c:if test="${empty maxlength}">
		<c:set var="maxlength" value="40" />
	</c:if>

	<c:if test="${empty deleteButton}">
		<c:set var="deleteButton" value="true" />
	</c:if>

	<!-- If edit parameter not set -->
	<c:if test="${empty edit}">
		<c:set var="edit" value="true" />
	</c:if>

	<!-- Default Date Pattern -->
	<c:if test="${empty time}">
		<c:set var="time" value="true" />
	</c:if>

	<c:if test="${time}">
		<c:set var="datePattern" value="#{messages['happyfaces.dateTimeFormat']}" />
	</c:if>

	<c:if test="${!time}">
		<c:set var="datePattern" value="#{messages['happyfaces.dateFormat']}" />
	</c:if>
	
	<!-- Default list type -->
	<c:if test="${empty listType}">
		<c:set var="listType" value="simpleList" />
	</c:if>
	<c:if test="${empty showButtonAddNew}">
		<c:set var="showButtonAddNew" value="true" />
	</c:if>

	<c:if test="${empty disabled}">
		<c:set var="disabled" value="false" />
	</c:if>

	<c:if test="${empty id}">
		<c:set var="id" value="#{field}" />
	</c:if>
	
	<c:if test="${show}">
		<!--  Initialize the value binding -->
		<bind:setValueBinding var="vb" valueBinding="#{entity[field]}" />
		<c:if test="${popup}">
			<c:set var="childEntity" value="#{entity[field]}" />
			<p:outputLabel for="#{id}" value="#{label}" />
			<h:panelGrid columns="3">
				<p:inputText rendered="#{edit and entity[field]!=null}" id="#{id}" value="#{entity[field]}" required="${required}" size="#{size}" disabled="true" />
				<h:inputHidden rendered="#{edit and entity[field]==null}" required="${required}" />
				<p:inputText rendered="#{edit and entity[field]==null}" id="#{id}" value="" size="#{size}" required="${required}" disabled="true" />
				<p:commandLink rendered="#{edit}" id="selectLink">
					<h:graphicImage value="/img/search.gif" style="border:0" />
				</p:commandLink>
				<p:tooltip for="selectLink" value="#{messages['commons.search']}" showEffect="slide" hideEffect="slide" />
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
			</h:panelGrid>
		</c:if>
		<c:if test="${!popup}">
			<!--  If the value binding is a string, display an inputText field. -->
			<bind:isText id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<c:if test="${empty textArea}">
					<p:inputText rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}" size="#{size}"
						maxlength="#{maxlength}" />
				</c:if>
				<c:if test="${!empty textArea}">
					<p:inputTextarea rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}" cols="#{cols}" rows="#{rows}"
						maxlength="#{maxlength}" />
				</c:if>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isText>

			<!--  If the value binding is a number, display an input number spinner. -->
			<bind:isInteger id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:spinner rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}"/>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isInteger>

			<bind:isByte id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:spinner rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}"/>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isByte>
			
			<bind:isLong id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:spinner rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}"/>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isLong>

			<bind:isShort id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:spinner rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}"/>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isShort>

			<!--  If the value binding is a rational number, display an input text field. -->
			<bind:isBigDecimal id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:inputText rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}" size="5" maxlength="#{maxlength}" />
				<c:if test="${empty converterParam}">
					<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				</c:if>
				<ui:insert />
			</bind:isBigDecimal>
			
			<bind:isDouble id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:inputText rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}" size="5"/>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isDouble>

			<bind:isFloat id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:inputText rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}" size="5" maxlength="#{maxlength}"/>
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isFloat>

			<!--  If the value binding is a boolean, display a selectBooleanCheckbox field. -->
			<bind:isBoolean id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />  

				<p:selectBooleanCheckbox id="#{id}" rendered="#{edit}" disabled="#{disabled}" value="#{entity[field]}" required="${required}">
					<c:if test="${!empty event}">
						<p:ajax event="onclick" update="#{reRender}" />
					</c:if>
				</p:selectBooleanCheckbox>

				<c:if test="${!edit and isMessage}">
					<c:set var="fieldValue" value="#{entity[field]}" />
					<h:outputText rendered="#{fieldValue != null and fieldValue.toString() == 'true' and !edit}" value="#{messages['commons.yes']}" style="font-weight:bold;" />
					<h:outputText rendered="#{fieldValue != null and fieldValue.toString() == 'false' and !edit}" value="#{messages['commons.no']}" style="font-weight:bold;" />
					<!-- toString was some workaround - todo: maybe works without toString  -->
					<h:outputText rendered="#{fieldValue != null and fieldValue.toString() != 'true' and fieldValue.toString() != 'false' and !edit}" value="#{messages[entity[field]]}" style="font-weight:bold;" />
				</c:if>

				<c:if test="${!edit and !isMessage}">
					<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;" />
				</c:if>

				<ui:insert />
			</bind:isBoolean>

			<!--  If the value binding is a date, display a rich:calendar field. -->
			<bind:isDate id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />
				<p:calendar id="#{id}" rendered="#{edit}" value="#{entity[field]}" disabled="#{disabled}" pattern="#{datePattern}" required="${required}" />
				<h:outputText rendered="#{!edit}" value="#{entity[field]}" style="font-weight:bold;">
					<f:convertDateTime type="date" pattern="#{datePattern}" />
				</h:outputText>
				<ui:insert />
			</bind:isDate>

			<!--  If the value binding is an enum, display a drop down. -->
			<bind:isEnum id="vb">
				<p:outputLabel for="#{id}" value="#{label}" />  
				<p:selectOneMenu rendered="#{edit}" id="#{id}" disabled="#{disabled}" value="#{entity[field]}" required="${required}">
						<f:selectItem rendered="#{required}" itemLabel="" itemValue="" />
						<f:selectItems value="#{vbType.getEnumConstants()}" var="enumElement" itemLabel="#{messages[enumElement.label]}" />
					<c:if test="${!empty event}">
						<p:ajax event="onchange" reRender="#{reRender}" />
					</c:if>
				</p:selectOneMenu>
				<c:if test="${!edit}">
					<c:set var="enumElement" value="#{entity[field]}" />
				</c:if>
				<h:outputText id="#{id}_view" rendered="#{!edit}" value="#{messages[enumElement.label]}" style="font-weight:bold;" />
				<ui:insert />
			</bind:isEnum>

			<!--  If the value binding is a List, display a panel with list of entities. -->
			<bind:isList id="vb">

				<c:if test="#{listType == 'simpleList'}">
					<h:outputLabel value="#{label}" />
					<h:panelGrid>
						<p:dataTable id="datatable" value="#{entity[field]}" var="listElement">
							<c:forEach items="#{showFields.split(';')}" var="fieldToShow">
								<c:set var="headerAndValue" value="#{fieldToShow.split(',')}" />
								<c:set var="style" value="text-align:left;" />
								<c:if test="${'right'.equals(headerAndValue[2])}">
									<c:set var="style" value="text-align:right;" />
								</c:if>
								<p:column width="100" style="${style}">
									<f:facet name="header">
										<h:outputText value="#{messages[headerAndValue[0]]}" />
									</f:facet>
									<c:set var="value" value="#{listElement[headerAndValue[1]]}"></c:set>
									<c:if test="${'msg'.equals(headerAndValue[2])}">
										<c:set var="value" value="#{messages[listElement[headerAndValue[1]].label]}" />
									</c:if>
									<h:outputText style="text-align:right;" value="#{value}" converter="#{getConverter.forType(listElement[headerAndValue[1]])}" />
								</p:column>
							</c:forEach>
	
							<c:if test="#{deleteButton}">
								<p:column rendered="#{edit}">
									<p:tooltip for="deletelink" value="#{messages['commons.delete']}" showEffect="slide" hideEffect="slide"/>
									<h:commandLink id="deletelink" action="#{backingBean.delete(listElement.id)}"
										onclick="if(confirm('#{messages['commons.confirmDelete']}')){return true;}else{return false;}">
										<h:graphicImage value="/img/delete.gif" style="border:0" />
									</h:commandLink>
								</p:column>
							</c:if>
						</p:dataTable>
						<c:if test="#{showButtonAddNew}">
							<p:commandButton rendered="#{edit and entity.getId() != null}" value="#{messages['commons.addNew']}" onclick="alert('not implemented. formField.xhtml line 248')" />
						</c:if>
							<ui:insert />
					</h:panelGrid>
				</c:if>

				<c:if test="#{listType=='pickUpList'}">
					<p:outputLabel for="#{id}" value="#{label}" />  
					<c:set var="entityField" value="#{entity[field]}" />
					
					<p:pickList id="#{id}" value="#{backingBean.dualListModel}" var="elem" itemLabel="#{elem[childField]}" itemValue="#{elem}" disabled="#{!edit}" >
						<f:converter binding="#{entityConverter}" />
					</p:pickList>  
					
				</c:if>
			</bind:isList>

		</c:if>
	</c:if>
</ui:composition>

</html>
